package com.skwen.android_room;

import android.os.Bundle;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.skwen.android_room.adapter.AdapterClickListener;
import com.skwen.android_room.adapter.BooksAdapter;
import com.skwen.android_room.bean.Books;
import com.skwen.android_room.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding mBinding;
    private BooksAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        mAdapter = new BooksAdapter();

        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mBinding.recyclerView.setAdapter(mAdapter);

        mAdapter.addClickListener(new AdapterClickListener() {
            @Override
            public void onItemClick(int position, Object data) {
                Books books = (Books) data;
                Toast.makeText(MainActivity.this, books.getName(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public boolean onItemLongClick(int position, Object data) {
                Books books = (Books) data;
                Toast.makeText(MainActivity.this, books.getPrice(), Toast.LENGTH_SHORT).show();
                return true;
            }
        });
    }


    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    private void getData() {
        mAdapter.setData(
                AppDatabase.getInstance(this)
                .booksDao()
                .getAll());
    }
}